package com.example.sdk.utils;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;


@Data
@ApiModel(description= "请求返回类型")
public class ReturnDTO<T> implements Serializable {

    private static final long serialVersionUID = 4145720818048417499L;
    @ApiModelProperty(value = "返回错误代码  0交易失败 1交易成功 2 部分失败（含义待添加,暂不用）")
    private String retCode;
    @ApiModelProperty(value = "交易流水号")
    private String seriesNo;
    @ApiModelProperty(value = "微服务名称")
    private String serName;
    @ApiModelProperty(value = "在交易错误情况下 具体错误信息")
    private String retMsg;
    @ApiModelProperty(value = "自己定义的返回内容 可以是任意对象、集合")
    private T retData;

    private JSONObject data;

    @ApiModelProperty(value = "aes的密匙（rsa加密后的密文）")
    private String aesKey;

    @ApiModelProperty(value = "返回的值（aes加密后的密文）")
    private String resultStr;


    public static ReturnDTO error() {

        return error(ResultEnum.FAIL.code, "未知异常，请联系管理员");

    }
    public static ReturnDTO error(TradeErrorEnum errorEnum) {

        return error(errorEnum.code,errorEnum.msg);

    }

    public static ReturnDTO error(String msg) {

        return error(ResultEnum.FAIL.code, msg);
    }

    public static<T> ReturnDTO error(String msg , T retData) {

        return error(ResultEnum.FAIL.code, msg,retData);
    }

    public static ReturnDTO error(String retCode, String msg) {
        ReturnDTO returnDTO = new ReturnDTO();
        returnDTO.setSerName(ResultEnum.SERVICE_NAME.code);
        returnDTO.setRetCode(retCode);
        returnDTO.setRetMsg(msg);
        return returnDTO;
    }

    @SuppressWarnings("unchecked")
    public static<T> ReturnDTO error(String retCode, String msg, T retData) {
        ReturnDTO returnDTO = new ReturnDTO();
        returnDTO.setSerName(ResultEnum.SERVICE_NAME.code);
        returnDTO.setRetCode(retCode);
        returnDTO.setRetMsg(msg);
        returnDTO.setRetData(retData);
        return returnDTO;
    }

    public static ReturnDTO ok() {
        ReturnDTO returnDTO = new ReturnDTO();
        returnDTO.setSerName(ResultEnum.SERVICE_NAME.code);
        returnDTO.setRetCode(ResultEnum.SUCCESS.code);
        return returnDTO;
    }

    public static  ReturnDTO ok2(JSONObject data){
        ReturnDTO returnDTO = new ReturnDTO();
        returnDTO.setSerName(ResultEnum.SERVICE_NAME.code);
        returnDTO.setRetCode(ResultEnum.SUCCESS.code);
        returnDTO.setData(data);
        return returnDTO;
    }
    @SuppressWarnings("unchecked")
    public static<T> ReturnDTO ok(T retData) {
        ReturnDTO returnDTO = new ReturnDTO();
        returnDTO.setSerName(ResultEnum.SERVICE_NAME.code);
        returnDTO.setRetCode(ResultEnum.SUCCESS.code);
        returnDTO.setRetData(retData);
        return returnDTO;
    }

    public static<T> ReturnDTO ok3(T retData, ReturnDTO dto) {
        dto.setRetData(retData);
        return dto;
    }
    @Override
    public String toString() {
        return "ReturnDTO{" +
                "retCode='" + retCode + '\'' +
                ", retMsg='" + retMsg + '\'' +
                ", seriesNo='" + seriesNo + '\'' +
                ", retData=" + retData +
                '}';
    }

}
